import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_redux/flutter_redux.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:wuxu_flutter_lianxi/redux/redux_login.dart';
import 'package:wuxu_flutter_lianxi/redux/wx_state.dart';


///
/// 主页面 侧边栏
///
class MainDrawer extends StatefulWidget {
  const MainDrawer({super.key});

  @override
  State<StatefulWidget> createState() => MainDrawerState();
}

class MainDrawerState extends State<MainDrawer> {

  @override
  Widget build(BuildContext context) {
    double width = MediaQuery
        .of(context)
        .size
        .width / 3;
    double screenHeight = MediaQuery
        .of(context)
        .size
        .height;
    return Container(
      width: width,
      height: screenHeight,
      color: Colors.white,
      padding: const EdgeInsets.only(top: 50, bottom: 20),
      child: Column(
        children: [
          _user(context),
          _itemButton("退出登录",bgColor: Colors.cyan,tap:(){
            Fluttertoast.showToast(msg: "退出登录");
            StoreProvider.of<WXState>(context).dispatch(LoginOutAction(context));
          }),
        ],
      ),
    );
  }

  Widget _user(BuildContext context){
    // UserInfo? userInfo = UserDao.userInfo;//不能直接用全局数据，应该从state中取
    var state = StoreProvider.of<WXState>(context).state;
    debugPrint(state.userInfo?.toString());
    return Container(
        alignment: Alignment.center,
        width: double.infinity,
        height: 100,
        color: Colors.blue,
        child: Row(
          children: [
            Image(
                width: 40,
                height: 40,
                errorBuilder: (BuildContext context, Object exception, StackTrace? stackTrace) {
                  return const Icon(Icons.ac_unit_sharp,size: 40,);
                },
                image: NetworkImage(state.userInfo?.avatar??"")),
            Text(state.userInfo?.nickname??"1111")
          ],
        ));
  }


  Widget _itemButton(String text,{MaterialColor bgColor=Colors.green,required Function tap}) {
    return GestureDetector(
        onTap: () {
          tap();
        },
        child: Container(
            alignment: Alignment.center,
            width: double.infinity,
            height: 50,
            color: bgColor,
            child: Text(text)));
  }
}
